2015F CST8277 MIDTERM #1 VERSION 080

One-Answer Multiple Choice
25 Questions - 7 of 15%

1. What is the only method declared by the Runnable interface?
	a. run (correct)
	b. doit
	c. start
	d. go
	e. worker

2. In order for a client and a server to establish a socket connection
	a. The server needs the port number, and the client needs both the port number and the server's IP address (correct)
	b. The server needs the port number, and the client needs both the port number and the client's IP address
	c. None of these answers
	d. The client needs the port number, and the server needs both the port number and the client's IP address
	e. The client needs the port number, and the server needs both the port number and the server's IP address

3. What are the two classes from the java.net package that are used to implement TCP socket communication between Java programs across a network?
	a. ClientSocket and ServerSocket
	b. None of these answers (correct)
	c. ClientSocket and Socket
	d. TCP and UDP
	e. SocketTCP and SocketUDP

4. In the Java RMI system, non-primitive Object parameters for remote method calls
	a. will cause an IOException
	b. are serialized and transmitted across the network (correct)
	c. will cause a RemoteException
	d. must have been exported to the RMI registry
	e. will cause a RemoteException unless they have been exported to the RMI registry

5. In a Java RMI application, a stub is
	a. a proxy for a remote object (correct)
	b. a remote method
	c. A proxy for a remote method
	d. A proxy for a remote JVM
	e. A remote object

6. Which statement about Java networking is true?
	a. A client uses the ServerSocket class to connect to a server
	b. A server uses the Socket class to listen on a port
	c. A client uses the ServerSocket class to listen on a port
	d. A server accepts a connection to a port at the server's IP address (correct)
	e. A server makes a connection on a port and a client listens on a port

7. To implement an RMI client application, the following method call is necessary to make initial contact with the server.
	a. LocateRegistry.createRegistry()
	b. All three of these answers
	c. Naming.lookup() (correct)
	d. None of these three answers
	e. UnicastRemoteObject.exportObject()

8. Which of the following lines of Java code would be run on the server side?
	a. ServerSocket serverSocket = new Socket();
	b. ServerSocket serverSocket = new ServerSocket(hostname);
	c. Socket serverSocket = new Socket(portNumber);
	d. ServerSocket serverSocket = new ServerSocket(hostname,portNumber);
	e. None of these answers (correct)

9. The Java code block public synchronized void doIt() {<statements>} would mean
	a. Multiple threads can run the object's doIt() method
	b. All three of these answers
	c. Only one thread at a time can run the doIt() method
	d. None of these answers (correct)
	e. Only one thread at a time can run <statements>

10. Which of the following statements is true?
	a. All three of these answers
	b. None of these three answers
	c. The Executor interface synchronizes the execution of threads
	d. The Java code for creating a Thread could be Thread t = Executors.newCachedThreadPool().getThread();
	e. The Executors class has a method newCachedThreadPool that returns an ExecutorService (correct)

11. Which of the following Java statements could result in a useful additional Thread of execution?
	a. new Thread(new Runnable(){public void run(){useful();}}.run();
	b. new Thread().start();
	c. None of these answers (correct)
	d. new Thread(new Runnable(){public void start(){useful();}}).run();
	e. new Thread().run();

12. It is possible to do Android Development and test Android Applications without a physical Android device because
	a. All of these answers
	b. Android apps can run on an Android Virtual Device (correct)
	c. None of these answers
	d. Eclipse has a plugin that will run Android code
	e. Android apps are written in Java, a multi-platform language

13. Both sides of a distributed Java RMI application can act as Server or Client in various operations. Which of the following is a Client action?
	a. Make references to remote objects available on the network
	b. All of these answers (correct)
	c. Wait for clients to invoke methods on remote objects
	d. Create remote objects
	e. Obtain a remote reference to one or more remote objects

14. The Java code block synchronized(someObject){<statements>} would mean
	a. Any thread executing <statements> must have acquired the monitor lock of someObject (correct)
	b. None of these answers
	c. Any thread executing <statements> will wait() for a notify() if it accesses someObject
	d. Any thread executing <statements> must have registered its own monitor lock
	e. All three of these answers

15. Which of the following is a properly formed RMI URL:
	a. rmi://1099:example.com/myservice
	b. None of these answers
	c. rmi://109.78.34.3:1099/myservice (correct)
	d. http://example.com/myservice
	e. http://example.com/1099:myservice

16. Remote method parameters are handled by the Java RMI system in the following way:
	a. None of these answers
	b. Passed by value for primitive types, by reference for object types
	c. Parameters are passed in the form of primitive data types
	d. Always passed by value (correct)
	e. Always passed by reference

17. Which of the following lines of Java code could be run on the server side to accept a connection from the client?
	a. ServerSocket s = serverSocket(portNum).accept();
	b. Socket s = new Socket(portNum).accept();
	c. ServerSocket s = new ServerSocket().accept();
	d. Socket s = serverSocket.accept(); (correct)
	e. Socket s = new Socket().accept();

18. Which of the following lines of Java code would be run on the client side?
	a. ServerSocket echoSocket = new Socket(hostName, portNumber);
	b. Socket echoSockey = new Socket(portNumber);
	c. Socket echoSocket = new Socket(hostName, portNumber); (correct)
	d. ServerSocket echoSocket = new ServerSocket(portNumber);
	e. None of these answers

19. Which of the following statements about the Java RMI Registry is true:
	a. The Registry binds remote objects to service names
	b. The Registry might be launched from the command line before the distributed application is launched
	c. All of these answers (correct)
	d. The Registry is accessed through methods of the Naming class
	e. The Registry maps service names to remote objects

20. Which of the following is NOT a valid, possibly partial, state sequence for a Java thread?
	a. Runnable -> Running -> Sleeping -> Runnable
	b. Running -> Blocked -> Runnable -> Running
	c. Runnable -> Running -> Blocked -> Runnable
	d. Runnable -> Running -> Blocked -> Running (correct)
	e. None of these answers

21. What is the synchronized Java keyword used for?
	a. None of these answers
	b. It is used to specify that a block of code runs simultaneously in more than one thread
	c. It is used to specify that a block of code should be run in a separate thread
	d. It is used to merge two threads into one
	e. It is used to specify that a block of code (or a method) requires an executing thread to first acquire a monitor lock (correct)

22. Which of the following statements is true?
	a. A thread will continue running until completion of every other thread is in the runnable state
	b. All three of these answers
	c. None of these answers
	d. A thread's priority starts out high and decreases as it runs
	e. A thread dies when its run() method completes (correct)

23. Which of the following statements is true?
	a. suspend(), resume(), and stop() are recommended for proper thread management
	b. notify(), notifyAll(), and wait() can only be called in synchronized code (correct)
	c. notify(), notifyAll(), and wait() cannot be called in synchronized code
	d. None of these three answers
	e. All three of these answers

24. To implement an RMI server application, the following method call is necessary to make a certain service available on a certain port:
	a. UnicastRemoteObject.exportObject()
	b. LocateRegistry.createRegistry()
	c. Naming.rebind() (correct)
	d. All three of these answers
	e. None of these three answers

25. Which of the following statements is true?
	a. A Java thread that issues a notifyAll() call enters the Blocked state
	b. A Java thread attempting to enter a synchronized block would issue a wait() call
	c. A Java thread attempting to enter a synchronized block would issue a notifyAll() call
	d. All three of these answers
	e. None of these three answers (correct)


Written Answer Section

Marks for each question are indicated, with a total of 25 Marks (half of 15%).
Question 1 asks you to adjust Java code. Correct answer(s) can be achieved by adding your lines of code to the lines provided, but you may rewrite entire method(s) in separate space if you choose. Missing import statements, minor syntax errors, and other minor errors are excusable.

1. Adjust the following Java code so that every loop iteration, that is, every call to worker.doProcessing(), is executed in its own thread. (5 marks)

public class Looper {
	private Worker worker = new Worker();
	void doLoop() {
		for (int i = 0; i < 100; i++) {


		}
	}
}


	public class Looper {
		private Worker worker = new Worker(); 
		void doLoop(){
			for (int i = 0; i < 100; i++) {
				new Thread( new Runnable() {
					@override
					public void run() {
						 worker.doProcessing(); 
					}
				}).start();
			}
		}
	}

2. Explain the purpose of the Java RMI registry and how it is used by the server and clients of a distributed architecture. Include examples of Java code in your explanation. (10 marks)

	The RMI registry is used by RMI servers (providers of remote objects) to bind remote objects to service names.  For example:
	Naming.rebind("rmi://localhost/ServiceName", remoteObjectReference);

	An RMI client uses the registry to look up a service name and retrieve a reference to a remote object that supplies that service.  For example

	MyRemoteInterface service = 
			   (MyRemoteInterface) Naming.lookup("rmi://servername.com/ServiceName");

	The client now has the remote reference corresponding to the server's "remoteObjectReference" variable in the client's "service" variable, and the client can now use it to invoke remote methods.
	
3. Explain what the synchronized keyword in Java is for, and the purpose of the wait and notifyAll methods. (10 marks)

	The synchronized keyword allows the programmer to control access by multiple threads to a block of code. Before a thread can execute a synchronized block or a synchronized method, that thread must acquire the monitor lock for the object mentioned in the synchronized block, or the object that supplies the method in the case of a synchronized method. This means only one thread at a time can access the synchronized code.

	The wait method is called only in synchronized code, by a thread that should wait for another thread to proceed. When this method is called by a thread, the monitor lock is released, and the thread goes into a blocked state until the notifyAll method is called by another thread, to notify the first thread that it should check again whether it should wait or proceed.